package com.android.test.activity;

import com.android.test.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class WelcomeActivity extends Activity {
	private final  static String TAG = WelcomeActivity.class.getSimpleName();
	private ProgressBar mProgressBar = null;
	private final int PROGRESS_SHOW = 1;
	private final int PROGRESS_DONE = 2;
	private int mProgress = 0;
	private CountDownTimer mCountDownTimer;
	private int mCountDownTime = 1500;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFormat(1);
		setContentView(R.layout.activity_welcome);
		final TextView msg_show = (TextView)findViewById(R.id.msg_welcome);
		final ProfilePreference profer = new ProfilePreference(this);
		final String userName = profer.getValueByKey(ProfilePreference.COLUMN_USERNAME);
		
		if(TextUtils.isEmpty(userName)){
			msg_show.setText(R.string.msg_welcome_not_registed);
		}else{
			final StringBuilder sb = new StringBuilder();
			sb.append(getString(R.string.msg_welcome_registed));
			sb.append(userName);
			sb.append(", ");
			sb.append(getString(R.string.msg_welcome_not_registed));
			msg_show.setText(sb.toString());
		}
	}

	@Override
	protected void onResume() {
		mHandler.sendEmptyMessage(PROGRESS_SHOW);
		super.onResume();
	}
	
	
	private Handler mHandler = new Handler(){
		public void handleMessage(Message msg){
			switch(msg.what){
			case PROGRESS_SHOW:
				Log.e(TAG, "Recive Message PROGRESS_SHOW ");
				showProgressBar();
				break;
			case PROGRESS_DONE:
				{
					Log.e(TAG, "Recive Message PROGRESS_DONE mProgress = "+mProgress);
					mCountDownTimer.cancel();
					//mProgressBar.setVisibility(View.GONE);
					finish();
					Intent it = new Intent();
					it.setClass(WelcomeActivity.this, MainEntryActivity.class);
					startActivity(it);
				}
				break;
			}
		}
	};

	
	private void showProgressBar() {
		mProgressBar = (ProgressBar)findViewById(R.id.circleProgressBar);
		mProgressBar.setVisibility(View.VISIBLE);
		mProgressBar.setProgress(0);
		mCountDownTimer = new CountDownTimer(mCountDownTime, mCountDownTime) {
			public void onTick(long millisUntilFinished) {
				Log.e(TAG, "onTick mProgress = "+mProgress);
			}

			public void onFinish() {
				Log.e(TAG, "onFinish mProgress = "+mProgress);
				mHandler.sendEmptyMessage(PROGRESS_DONE);
			}
		}.start();
	}
}
